package com.common;

import java.sql.*;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

/**
 *
 * @author Administrator
 *
 */
public class DB {
	private static BasicDataSource dataSource = null;
	protected static Statement s=null;
	protected static ResultSet rs = null;
	protected static Connection conn = null;
	 /**
     *
     */
    public static void init() 
    {
  	if (dataSource != null)
        {
            try
            {
                dataSource.close();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            dataSource = null;
        }

       
        try {
            Properties p = new Properties();
            p.setProperty("driverClassName", "com.mysql.jdbc.Driver");
            
            p.setProperty("url", "jdbc:mysql://localhost:3306/rjxy?useUnicode=true&characterEncoding=utf8");
           
           p.setProperty("username", "root");
            
           p.setProperty("password", "123");
            
           //   p.setProperty("url", "jdbc:mysql://w.suneasy.cn:3316/jsprun?useUnicode=true&characterEncoding=utf8");
            
           //   p.setProperty("username", "jsprun");
               
           //   p.setProperty("password", "jsprunjsprun");
             
            p.setProperty("removeAbandonedTimeout", "120"); 
            
            p.setProperty("maxActive", "100");
            p.setProperty("maxIdle", "50");
            p.setProperty("maxWait", "1024");
            
            
          
            
            dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 
     * @return
     * @throws SQLException
     */
    public static synchronized Connection getConnection() throws  SQLException {
        if (dataSource == null) {
            init();
        }
        if (dataSource != null) {
            conn = dataSource.getConnection();
        }
        return conn;
    }
	/**
	 *
	 * @param sql
	 * @return
	 */
	public static int executeUpdate(String sql)
	{
		int result = 0;
		try {
			s = getConnection().createStatement();
			result = s.executeUpdate(sql);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return result;
	}
	/**
	 *
	 * @param sql
	 * @return
	 */
	public static ResultSet executeQuery(String sql)
	{
		
		try {
			s = getConnection().createStatement();
			rs = s.executeQuery(sql);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return rs;
	}
	public static PreparedStatement executePS(String sql)
	{
		PreparedStatement ps = null;
		try
		{
			ps = getConnection().prepareStatement(sql);
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return ps;
	}
	
	public static void rollback() {
		try {
			conn.rollback();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
	}
	
	public static void close()
	{
		try
		{
			if(rs!=null)
				rs.close();
			if(s!= null)
				s.close();
			if(conn!=null)
				conn.close();
			
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
	}
	
}
